package hk.ust.comp3111.ustnutrition;

import java.io.InputStream;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.util.Log;
import android.widget.ImageView;

public class DownloadImageTask extends AsyncTask<String, Void, Bitmap>
{
	public static final String TAG = "hk.ust.comp3111.ustnutrition.DownloadImageTask";

    public DownloadImageTask(ImageView view, Drawable failImg)
    {
        this.view = view;
        this.failImg = failImg;
    	width = 0;
    	height = 0;
    }

    public DownloadImageTask(int width, int height)
    {
    	view = null;
    	failImg = null;
    	this.width = width;
    	this.height = height;
    }

    @Override
	protected Bitmap doInBackground(String... urls)
    {
        String urldisplay = urls[0];
        Bitmap bmp = null;
        InputStream in = null;
        try
        {
            in = new java.net.URL(urldisplay).openStream();
            BitmapFactory.Options op = new BitmapFactory.Options();
            op.inJustDecodeBounds = true;
            BitmapFactory.decodeStream(in, null, op);
            in.close();
            int wScale = 0, hScale = 0;
            if (width > 0)
            {
                while (op.outWidth > width)
                {
                	op.outWidth /= 2;
                	++wScale;
                }
                wScale = (int)Math.pow(2, wScale - 1);
            }
            if (height > 0)
            {
                while (op.outHeight > height)
                {
                	op.outHeight /= 2;
                	++hScale;
                }
                hScale = (int)Math.pow(2, hScale - 1);
            }

            BitmapFactory.Options op2 = new BitmapFactory.Options();
            if (width > 0 && height > 0)
            {
                op2.inSampleSize = Math.min(wScale, hScale);
            }
            else if (width > 0)
            {
                op2.inSampleSize = wScale;
            }
            else if (height > 0)
            {
                op2.inSampleSize = hScale;
            }
            Log.d(TAG, "Scale \""
            			+ urldisplay.substring(urldisplay.lastIndexOf('/') + 1)
            			+ "\": " + op2.inSampleSize);

            if (!isCancelled())
            {
                in = new java.net.URL(urldisplay).openStream();
                bmp = BitmapFactory.decodeStream(in, null, op2);
                in.close();
            }
        }
        catch (Exception e)
        {
        	if (in != null)
        	{
        		try
        		{
                	in.close();
        		}
        		catch (Exception e2)
        		{}
        	}
            Log.w(TAG, "Exception", e);
        }
        return bmp;
    }

    @Override
	protected void onPostExecute(Bitmap result)
    {
    	if (result == null)
    		view.setImageDrawable(failImg);
    	else
    		view.setImageBitmap(result);
    }

    private ImageView view;
    private Drawable failImg;
    private int width;
    private int height;
}
